package com.ztjava.tieba.service;

import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.ztjava.core.base.BaseService;
import com.ztjava.core.client.Client;
import com.ztjava.tieba.model.TiebaCatalog;
import com.ztjava.tieba.model.TiebaContent;
import com.ztjava.tieba.model.TiebaReply;
import com.ztjava.tieba.model.TiebaSubject;

public class TiebaService extends BaseService{
	/*************
	 *Subject分页
	**************/
	public Page<TiebaSubject> getSubjectPage(Client client,Map<String,String[]> paramMap){
		String[][] param = new String[][]{
				new String[]{"name",like,"name"}				
				,new String[]{"code",like,"code"}				
				,new String[]{"enable_flag",eq,"enable_flag"}			
		};
		Page<TiebaSubject> pd = TiebaSubject.dao.paginate(
				getParaToInt(paramMap, client.getPageNumber(), 1), 
				getParaToInt(paramMap,client.getPageSize(),20),
				"select *","from "+TiebaSubject.tablename+" where 1=1 " 				
				+ whereSql(paramMap,param)
				+ orderby(client, paramMap),whereParam(paramMap, param));
		changeBasedata(pd.getList()		, new String[]{"enable_flag","ENABLE_FLAG"});
		return pd;
	}
	
	public TiebaSubject getSubjectByCode(String code){
		return TiebaSubject.dao.findFirst("select * from "+TiebaSubject.tablename+" where code=?",code);
	}
	
	/*************
	 *Content分页
	**************/
	public Page<TiebaContent> getContentPage(Client client,Map<String,String[]> paramMap){
		String[][] param = new String[][]{
				new String[]{"subject_id",eq,"subject_id"}		
				,new String[]{"catalog_id",eq,"catalog_id"}
				,new String[]{"name",like,"name"}				
		,new String[]{"is_pass",eq,"is_pass"}				
		,new String[]{"enable_flag",eq,"enable_flag"}			};
		Page<TiebaContent> pd = TiebaContent.dao.paginate(
				getParaToInt(paramMap, client.getPageNumber(), 1), 
				getParaToInt(paramMap,client.getPageSize(),20),
				"select *","from "+TiebaContent.tablename+" where 1=1 " 				
				+ whereSql(paramMap,param)
				+ orderby(client, paramMap),whereParam(paramMap, param));
		changeBasedata(pd.getList()		, new String[]{"is_pass","SHIFOU"}		
		, new String[]{"enable_flag","ENABLE_FLAG"}	
		, new String[]{"subject_id","TIEBA_SUBJECT"}	
		, new String[]{"catalog_id","TIEBA_CATALOG"});
		changeUser(pd.getList(), new String[]{"create_user","name"},
				new String[]{"create_user","headimg"});
		return pd;
	}
	/*************
	 *Catalog分页
	**************/
	public Page<TiebaCatalog> getCatalogPage(Client client,Map<String,String[]> paramMap){
		String[][] param = new String[][]{
		new String[]{"name",like,"name"}				
		,new String[]{"enable_flag",eq,"enable_flag"}		
		,new String[]{"subject_id",eq,"subject_id"}	};
		Page<TiebaCatalog> pd = TiebaCatalog.dao.paginate(
				getParaToInt(paramMap, client.getPageNumber(), 1), 
				getParaToInt(paramMap,client.getPageSize(),20),
				"select *","from "+TiebaCatalog.tablename+" where 1=1 " 				
				+ whereSql(paramMap,param)
				+ orderby(client, paramMap),whereParam(paramMap, param));
		changeBasedata(pd.getList()		, new String[]{"enable_flag","ENABLE_FLAG"});
		return pd;
	}
	
	public List<TiebaCatalog> getCatalogList(Integer subject_id){
		return TiebaCatalog.dao.find("select * from "+TiebaCatalog.tablename+" where subject_id=? and enable_flag='T' order by sort asc,id asc",subject_id);
		
	}
	/*************
	 *Reply分页
	**************/
	public Page<TiebaReply> getReplyPage(Client client,Map<String,String[]> paramMap){
		String[][] param = new String[][]{
		new String[]{"is_pass",eq,"is_pass"}				
		,new String[]{"enable_flag",eq,"enable_flag"}			
		,new String[]{"content_id",eq,"content_id"}				
		,new String[]{"parent_id",eq,"parent_id"}			};
		Page<TiebaReply> pd = TiebaReply.dao.paginate(
				getParaToInt(paramMap, client.getPageNumber(), 1), 
				getParaToInt(paramMap,client.getPageSize(),20),
				"select *","from "+TiebaReply.tablename+" where 1=1 " 				
				+ whereSql(paramMap,param)
				+ orderby(client, paramMap),whereParam(paramMap, param));
		changeBasedata(pd.getList()		, new String[]{"is_pass","SHIFOU"}		
			, new String[]{"enable_flag","ENABLE_FLAG"});
		changeUser(pd.getList(), new String[]{"create_user","name"},
				new String[]{"create_user","headimg"});
		return pd;
	}

	public void updateContentReply(Integer content_id){
		Integer m = Db.queryInt("select count(id) from "+TiebaReply.tablename+" where content_id=?",content_id);
		if(m==null) m=0;
		Db.update("update "+TiebaContent.tablename+" set reply=? where id=?",m,content_id);
	}
}
